import request from '@/utils/request'
// @ts-ignore
import {BcTemplateModel, BcTemplateStateEnum} from "@/model/BcTemplateModel";
import {PagingModel} from "@/model/PagingModel";

export default class TemplateApi {

    public static getTemplateById(templateId: string): Promise<BcTemplateModel> {
        return new Promise<BcTemplateModel>((resolve, reject) => {
            request({
                url: `/broadcast/template/${templateId}`,
                method: 'get'
            }).then((res: any) => {
                if (res) {
                    resolve(new BcTemplateModel().deserialize(res))
                } else {
                    reject(res);
                }
            })
        })
    }

    /**
     * 查询配置列表
     *
     * @param searchKey
     * @param state
     * @param pageIndex
     * @param pageSize
     */
    public static listTemplate(deptId: string, searchKey: string,  pageIndex: number, pageSize: number): Promise<PagingModel<BcTemplateModel>> {
        return new Promise<PagingModel<BcTemplateModel>>((resolve, reject) => {
            request({
                url: '/broadcast/template/list',
                method: 'get',
                params: {
                    searchKey,
                    deptId,
                    pageNum: pageIndex,
                    pageSize
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    res.data = res.rows.map(item => new BcTemplateModel().deserialize(item))
                    res.total = +res.total
                    res.pageIndex = pageIndex
                    res.pageSize = pageSize
                    resolve(res)
                }
                reject(res);
            })
        })
    }

    /**
     * 创建字典
     * @param template
     */
    public static saveTempalte(template: BcTemplateModel): Promise<BcTemplateModel> {
        return new Promise<BcTemplateModel>((resolve, reject) => {
            request({
                url: `/broadcast/template/save`,
                method: 'POST',
                data: template
            }).then((res: any) => {
                if (res) {
                    resolve(new BcTemplateModel().deserialize(res))
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

    /**
     * 基于文件创建
     * @param template
     */
    public static saveTemplateByFile(template: any): Promise<BcTemplateModel> {
        return new Promise<BcTemplateModel>((resolve, reject) => {
            request({
                url: `/broadcast/template/save/by/file`,
                method: 'POST',
                data: template
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new BcTemplateModel().deserialize(res.data))
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

    /**
     * 基于文件插播
     * @param template
     */
    public static insertTemplateByFile(template: any): Promise<Boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/template/insert/by/file`,
                method: 'POST',
                data: template
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res)
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }


    /**
     * 基于计划插播
     * @param template
     */
    public static insertTemplateBySchedule(scheduleId: string, deviceId: string): Promise<Boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/template/insert/by/schedule`,
                method: 'POST',
                params: {
                    scheduleId,
                    deviceId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res)
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

    /**
     * 更新字典
     * @param id
     * @param template
     */
    public static updateTemplate(id:string, template: BcTemplateModel): Promise<BcTemplateModel> {
        return new Promise<BcTemplateModel>((resolve, reject) => {
            request({
                url: `/broadcast/template/${id}`,
                method: 'PUT',
                data: template
            }).then((res: any) => {
                if (res) {
                    resolve(new BcTemplateModel().deserialize(res))
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

    public static deleteTemplate(id:string): Promise<boolean> {
        return new Promise<boolean>((resolve, reject) => {
            request({
                url: `/broadcast/template/${id}`,
                method: 'DELETE',
            }).then((res: any) => {
                if (res) {
                    resolve(res)
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

    public static copyTemplate(id:string): Promise<BcTemplateModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/template/${id}/copy`,
                method: 'put',
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new BcTemplateModel().deserialize(res.data))
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

}

